package cn.bdqn.controller;

import cn.bdqn.pojo.Video;
import cn.bdqn.service.VideoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VideoController {
    @Resource
    VideoService videoService;
    @RequestMapping("/index")
    public String index(){
        return "list";
    }
    @ResponseBody
    @RequestMapping("/list")
    public Object list(String category,  Long languageId,  Integer pageIndex){
        Integer pageSize=2;
        int totalCount = videoService.selectCount(category, languageId);
        int totalPage=0;
        if (totalCount%pageSize==0){
            totalPage=totalCount/pageSize;
        }else{
            totalPage=totalCount/pageSize+1;
        }
        //防止突出
        if (pageIndex<1){
            pageIndex=1;
        }
        if (pageIndex>totalPage){
            pageIndex=totalPage;
        }
        List<Video> lists = videoService.selectAll(category, languageId,pageIndex,pageSize);

        Map<String,Object> map=new HashMap<>();
        map.put("list",lists);
        map.put("pageIndex",pageIndex);
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);

        return map;
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Video video){
        int rs = videoService.insert(video);
        Map<String,Object> map=new HashMap<>();
        if (rs>0){
            map.put("reult",true);
        }else{
            map.put("reult",false);
        }
        return map;
    }
    @RequestMapping("/modiaf")
    public String modiaf(Long id){
        return "modifay";
    }
    @RequestMapping("/selectOne")
    @ResponseBody
    public Video selectOne(Long id){
        Video video = videoService.selectOne(id);
        return video;
    }
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Video video){
        int rs = videoService.update(video);
        Map<String,Object> map=new HashMap<>();
        if (rs>0){
            map.put("reult",true);
        }else{
            map.put("reult",false);
        }
        return map;
    }
}
